$(function(){
	
	var newsCategoryId = null; 

	function NoticeModal(modalEle) {
		var self = this;
		self.$ele = modalEle;
		$('.save-btn',self.$ele).on('click',function(){
			self._onSave();
		});	
		$('.cancel-btn',self.$ele).on('click',function() {
			self._onCancel();
		});	
		self._initModal();
		self._initValidate();
	}
	NoticeModal.prototype = {
		constructor: NoticeModal,
		$ele: null,		
		$permissionBox: null,
		$selectBox: null,
		$validater: null,
		tagBox: null,
		_initModal: function () {
			$(this.$ele).modal({
				backdrop: 'static',
				keyboard: false,	
				show: false
			});
		},
		_initValidate: function() {
			this.validater = $('#add-guest-form').validate({
				rules: {
					"name": "required",
					"department ": "required",
					"description": "required",
					"image" : "required",
				},
				messages:{
					"name": {
						required: "<font color='red'>请输入分类名称</font>",
					},
					"description": {
						required: "<font color='red'>请输入描述信息</font>",
					},
				},
				success: function(label,ele) {
							 console.log(label);
							 console.log(ele);
						 },
				submitHandler: function(){
							saveNotice();
						   }
				
			});
	   },
		_onCancel: function() {
			$(this.$ele).modal('hide');
		},
		_onSave: function() {
			$('form',this.$ele).submit();
		},
		_reset: function () {
			$('#guest-modal input:text').each(function(){
				$(this).val('');
			});
			$('.has-error').removeClass('has-error');
			$('textarea[name="description"]',this.$ele).val('');
			$('#guest-avatar-upload').css("background-image","url()");
		},
		close: function () {
			$(this.$ele).modal('hide');
		},
		open: function (newsCategory) {
			this._reset();
			if(newsCategory) {
				$('.modal-title',this.$ele).html('编辑资讯分类列表');
				$('textarea[name="description"]',this.$ele).val(newsCategory["description"]);
				$('#guest-avatar-upload').css("background-image","url(" +newsCategory['image']+")");
				for(var key in newsCategory) {
					$('input[name="'+key+'"]',this.$ele).val(newsCategory[key]);
				}
			} else {
				$('.modal-title',this.$ele).html('新建资讯分类列表');
			}
			$(this.$ele).modal('show');
		}
	};

	/*
	 * 图片上传
	 */
	var avatarUploader = new PicUploader({
		before: function() {
			loading.show();	
		},
		success: function(obj) {
			var image = obj['data'];
			$('#avatar-btn').css('background-image','url('+image+')');
			$('#avatar-url').val(image);
			loading.hide();
		}
	});
	$('#avatar-btn').on('click',function(){
		avatarUploader.start();
	});

	var categoryCreater = new NoticeModal($('#guest-modal'));

	$('#add-guest-btn').on('click',function(){
		newsCategoryId = null;
		categoryCreater._reset();
		categoryCreater.open();
		
	});

	
	$('.edit-btn').on('click',function(){
		var $tr = $(this).parents('tr');
		newsCategoryId = $tr.attr('data-news-id');
		cAjax({
			url: '/web/news/getNewsCategory',  //*
			type: 'get',
			data: {
				id: newsCategoryId 
			},
			success: function(obj) {
				categoryCreater.open(obj.data);
			},
			error: function(obj) {
				console.log(obj);
		    }
		});
	});

	function saveNotice() {
		var data = {},attrs={},
			url = '';
		data['categoryId'] = $("#add-guest-form").attr("data-cid");
	    data['name'] = $.trim($('#name').val());
	    data['department'] = $.trim($('#department').val());
	    data['image'] = $.trim($('#image').val());
	    data['description'] = $('#description').val();
	    console.log();
		if(newsCategoryId == null) {
			url = '/web/news/addNewsCategory'; 
		} else {
			data['id'] = newsCategoryId;
			url = '/web/news/editNewsCategory'; 
		}
		if(data['image'].length==0||data['image']==""){
			greeting({msg:"请先上传主题图"});
			return false;
		}
		categoryCreater.close();
		cAjax({
			url: url,
			data: data,
			success: function(obj) {
				console.log(obj);
				greeting({
					msg: "资讯分类列表操作成功",
					confirm: function(){
						window.location.reload();
					}

				});
			},
			error: function(obj){
				console.log(obj);
		   }
		});
	}

	$('#guest-avatar-upload').on('click',function(){
		var $this = $(this);
		var uploader = new PicUploader({
			success:function(obj) {
				var url = obj['data'];
				$this.css('background-image','url('+url+')');
				//$this.next('[name="image"]').val(url);	
				$("#image").val(url);
			}
		});
		uploader.start();
	});

	$('.news-delete-btn').on('click',function(){
		var $this = $(this);
		actionConfirm({
			msg: "确定要禁用吗？",
			confirm: function() {
				cAjax({
					url:'/web/news/deleteNewsCategory',
					type:'post',
					data:{id: $this.parents('tr').attr('data-news-id')},
					success:function(obj){
						$this.parents('tr').addClass('deleted');
						$this.parent() .html('<span class="label label-warning">已禁用</span>');
//						$this.parent() .html('<a href="javascript:;" class="btn btn-xs btn-danger news-delete-btn">禁用</a>');
					},
					error: function(obj) {
					}
				});
			}
		});
	});
});


